import re
import time
import requests
from lxml import etree

class JQTE:
    """
    使用该类查询数量经济技术经济研究的稿件状态
    """
    def __init__(self):
        self.session = requests.Session()
        self.login_url = 'http://www.jqte.net/sljjjsjjyj/ch/author/login.aspx'
        self.check_paper_status_url = 'http://www.jqte.net/ajax/author/query_article_list_data.aspx'
        self.headers = {
                'Accept': '*/*',
                'Accept-Encoding': 'gzip, deflate',
                'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
                'Connection': 'keep-alive',
                'Content-Type': 'application/x-www-form-urlencoded',
                'Host': 'www.jqte.net',
                'Referer': 'http://www.jqte.net',
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36 Edg/85.0.564.44',
        }

    def login(self, username: str, password: str) -> None:
        """
        实现模拟登录
        @param username: 用户名
        @param password: 密码
        """
        # 第一次打开登录页面获取一些必要参数
        resp = self.session.get(url=self.login_url, headers=self.headers).text
        html = etree.HTML(resp)
        form_data = {
                '__VIEWSTATE': html.xpath(r'//input[@id="__VIEWSTATE"]/@value')[0],
                '__VIEWSTATEGENERATOR': html.xpath(r'//input[@id="__VIEWSTATEGENERATOR"]/@value')[0],
                '__VIEWSTATEENCRYPTED': html.xpath(r'//input[@id="__VIEWSTATEENCRYPTED"]/@value')[0],
                'UserName': username,
                'Hash': html.xpath(r'//input[@id="Hash"]/@value')[0],
                'Password': password,
                'Submit': '登 录',
                }
        # 获取到必备参数以后就可以开始模拟登录了
        resp = self.session.post(url=self.login_url, headers=self.headers, data=form_data, allow_redirects=False)
        location = resp.headers['Location']
        self.psu = re.search(r'psu=(.*?)&', location).group(1)
        self.jid = re.search(r'jid=(.*?)&', location).group(1)


    def check_paper_status(self) -> None:
        """
        检查稿件状态
        """
        before_query_url = 'http://www.jqte.net/ajax/author/query_article_list_ui.aspx'
        params = {
                'my_menu_id': '102',
                'psu': self.psu,
                'jid': self.jid,
                '_': str(int(time.time()*1000))
            }
        resp = self.session.get(url=before_query_url, headers=self.headers, params=params).text
        current_url = re.search(r'current_url=(.*?)",', resp).group(1)
        
        # 正式开始查询稿件状态
        time_stamp = str(int(time.time()*1000))
        params = {
                'op_type': 'list',
                'psu': self.psu,
                'my_menu_id': '102',
                'jid':  self.jid,
                'current_url': current_url,
                '_': time_stamp,
                '_search': 'false',
                'nd': time_stamp,
                'rows': '12',
                'page': '1',
                'sidx': '',
                'sord': 'desc',
        }
        resp = self.session.get(url=self.check_paper_status_url, headers=self.headers, params=params).json()
        if resp['total'] == '0':
            print('老铁先投稿再用脚本查看稿件状态吧！')
        else:
            for item in resp['rows']:
                content = item['cell']
                print(f'您的稿件编号为 {item["id"]} 的稿件:')
                print(f'文章标题为 《{content[5]}》')
                print(f'投稿时间为 {content[6]}')
                print(f'当前稿件状态为 {content[7]}\n')
            print('您所有稿件的状态都已汇报完毕，祝您早日见刊！')


if __name__ == '__main__':
    app = JQTE()
    app.login('your username', 'your password')
    app.check_paper_status()
